﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.Folder", ServerTypeId="{dbe8175a-505d-4eff-bec4-6c809709808b}")]
    public class Folder : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public Folder(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public void DeleteObject()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "DeleteObject", null);
            base.Context.AddQuery(query);
            base.RemoveFromParentCollection();
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                switch (peekedName)
                {
                    case "ParentFolder":
                        flag = true;
                        reader.ReadName();
                        this.ParentFolder.FromJson(reader);
                        return flag;

                    case "ItemCount":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["ItemCount"] = reader.ReadInt32();
                        return flag;

                    case "Name":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Name"] = reader.ReadString();
                        return flag;

                    case "ServerRelativeUrl":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["ServerRelativeUrl"] = reader.ReadString();
                        return flag;

                    case "WelcomePage":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["WelcomePage"] = reader.ReadString();
                        return flag;

                    case "Files":
                        flag = true;
                        reader.ReadName();
                        this.Files.FromJson(reader);
                        return flag;

                    case "Folders":
                        flag = true;
                        reader.ReadName();
                        this.Folders.FromJson(reader);
                        return flag;

                    case "UniqueContentTypeOrder":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["UniqueContentTypeOrder"] = reader.ReadList<ContentTypeId>();
                        return flag;

                    case "ContentTypeOrder":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["ContentTypeOrder"] = reader.ReadList<ContentTypeId>();
                        return flag;
                }
            }
            return flag;
        }

        [Remote]
        public ClientResult<Guid> Recycle()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "Recycle", null);
            base.Context.AddQuery(query);
            ClientResult<Guid> result = new ClientResult<Guid>();
            base.Context.AddQueryIdAndResultObject(query.Id, result);
            base.RemoveFromParentCollection();
            return result;
        }

        [Remote]
        public void Update()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "Update", null);
            base.Context.AddQuery(query);
        }

        [Remote]
        public IList<ContentTypeId> ContentTypeOrder
        {
            get
            {
                base.CheckUninitializedProperty("ContentTypeOrder");
                return (IList<ContentTypeId>) base.ObjectData.Properties["ContentTypeOrder"];
            }
        }

        [Remote]
        public FileCollection Files
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("Files", out obj2))
                {
                    return (FileCollection) obj2;
                }
                FileCollection files = new FileCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "Files"));
                base.ObjectData.ClientObjectProperties["Files"] = files;
                return files;
            }
        }

        [Remote]
        public FolderCollection Folders
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("Folders", out obj2))
                {
                    return (FolderCollection) obj2;
                }
                FolderCollection folders = new FolderCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "Folders"));
                base.ObjectData.ClientObjectProperties["Folders"] = folders;
                return folders;
            }
        }

        [Remote]
        public int ItemCount
        {
            get
            {
                base.CheckUninitializedProperty("ItemCount");
                return (int) base.ObjectData.Properties["ItemCount"];
            }
        }

        [Remote]
        public string Name
        {
            get
            {
                base.CheckUninitializedProperty("Name");
                return (string) base.ObjectData.Properties["Name"];
            }
        }

        [Remote]
        public Folder ParentFolder
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("ParentFolder", out obj2))
                {
                    return (Folder) obj2;
                }
                Folder folder = new Folder(base.Context, new ObjectPathProperty(base.Context, base.Path, "ParentFolder"));
                base.ObjectData.ClientObjectProperties["ParentFolder"] = folder;
                return folder;
            }
        }

        [Remote]
        public string ServerRelativeUrl
        {
            get
            {
                base.CheckUninitializedProperty("ServerRelativeUrl");
                return (string) base.ObjectData.Properties["ServerRelativeUrl"];
            }
        }

        [Remote]
        public IList<ContentTypeId> UniqueContentTypeOrder
        {
            get
            {
                base.CheckUninitializedProperty("UniqueContentTypeOrder");
                return (IList<ContentTypeId>) base.ObjectData.Properties["UniqueContentTypeOrder"];
            }
            set
            {
                base.ObjectData.Properties["UniqueContentTypeOrder"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "UniqueContentTypeOrder", value));
                }
            }
        }

        [Remote]
        public string WelcomePage
        {
            get
            {
                base.CheckUninitializedProperty("WelcomePage");
                return (string) base.ObjectData.Properties["WelcomePage"];
            }
            set
            {
                if (base.Context.ValidateOnClient && (value == null))
                {
                    throw ClientUtility.CreateArgumentNullException("value");
                }
                base.ObjectData.Properties["WelcomePage"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "WelcomePage", value));
                }
            }
        }
    }
}

